<?php 
	namespace frontend\controllers;
	use yii;
	use frontend\models\Companys;
	use frontend\models\Company;
	use frontend\models\Jobs;
	use yii\web\Controller;
	use yii\web\Cookie;
	// use yii\web\UploadedFile;
	class CompanysController extends Controller{
		public $layout='index';
		public function actionIndex()
		{
			//判断是否登录
			$cookie = Yii::$app->request->cookies;
			$model = new Jobs();
			//直接返回Cookie的值
			$user = $cookie->getValue('user1'); //$cookie[‘smister’] 其实这样也是可以读取的
			// var_dump($user);die;
			// print_r($user);die;
			//判断一个Cookie是否存在
			if ( $user  ) {
				//到首页
				return $this->render('job',['model'=>$model]);
			}else{
				//到登录
				echo "<script>alert('请确认后再输入');location.href='http://www.zhangsan.com/Nine/advanced/frontend/web/?r=companys/login'</script>";
			}
			
		}

		//企业登录
		public function actionLogin()
		{
			$model = new Company();
			//实例化验证类
			$request = Yii::$app->request;
			//接收数据
			$post = $request->post(); 
			if (Yii::$app->request->isPost && $model->load($post)) {
				// echo 1;die;
				$result = Yii::$app->db->createCommand("SELECT * FROM Company where username='".$post['Company']['username']."' and password='".$post['Company']['password']."' ")->queryOne();

				// print_r($result);die;
	            if ($result) {
	            	// 添加cookie 操作
					$cookies = Yii::$app->response->cookies;
					// var_dump($result);die;
					$id = $result['id'];
					
					$cookie_data = ['name' => 'user1', 'value' => $id];
					// print_r($cookie_data);die;
					$cookies->add(new Cookie($cookie_data));

	            	$this->redirect('?r=companys/job');
	            }else{
	            	echo "<script>alert('账号或密码输入错误');location.href='http://www.zhangsan.com/Nine/advanced/frontend/web/?r=companys/login'</script>";
	            }

			}else{
				return $this->render('login',['model' => $model]);
			}
		}

		//企业注册
		public function actionReg(){
			$model = new Company();
			$request = Yii::$app->request;
			
			$post = $request->post();

			if($request->isPost && $model->load($post))
			{		
				// print_r($post);die;
				$model->save(false);
				$id = Yii::$app->db->getLastInsertID();
				$cookies = Yii::$app->response->cookies;
				$cookie_data = ['name' => 'user1', 'value' => $id];
				// print_r($cookie_data);die;
				$cookies->add(new Cookie($cookie_data));
				$this->redirect('?r=companys/index');
			}
			return $this->render('index',['model'=>$model]);
		}

	    //企业发布招聘
	    public function actionJob()
	    {
	    	// echo 1;die;
	    	$model = new Jobs();
	    	$request = Yii::$app->request;
			$post = $request->post();

			if($request->isPost && $model->load($post))
			{		
				$cookie = Yii::$app->request->cookies;
				//直接返回Cookie的值
				$company_id = $cookie->getValue('user1'); 
				
				$create_time = date('Y-m-d H:i:s');
				$post['Jobs']['company_id'] = $company_id;
				$post['Jobs']['create_time'] = $create_time;
				// print_r($post);die;
				$model->company_id = $post['Jobs']['company_id'];
				$model->create_time = $post['Jobs']['create_time'];
				$model->save(false);
				$this->redirect('?r=talk/jobs');
			}
			return $this->render('job',['model'=>$model]);
	    }

	    //三级联动
		public function actions()
	    {
	        $actions=parent::actions();
	        $actions['get-region']=[
	            'class'=>\chenkby\region\RegionAction::className(),
	            'model'=>\frontend\models\Region::className(),
	        ];
	        return $actions;
	    }

	    //删除
	    public function actionDel()
	    {
	    	$request = Yii::$app->request;
	    	$id = $request->get('id');
	    	// print_r($id);die;
	    	$model = Jobs::findOne($id);
	    	// print_r($model);die;
			$model->delete();
	    	$this->redirect('?r=talk/jobs');
	    }

	    //修改
	    public function actionUpdate()
	    {
	    	
	    	$request = Yii::$app->request;

	    	$id = $request->get('id');

	    	$model = Jobs::findOne($id);

	    	$post = $request->post();
	    	
			// print_r($model);die;
	    	if($request->isPost)
	    	{	
	    		$pid = $post['Jobs']['id'];
				$model = Jobs::findOne($pid);
	    		$model->load($post);
	    		// echo 1;die;
	    		$model->save(false);
				// $this->redirect('?r=talk/jobs');
	    	}
	    	return $this->render('update',['model'=>$model]);
	    }

	}


?>